(defparameter *dict_path* "/usr/share/dict/words")
(defparameter *dict* nil)

(defun read-dict (path) 
  (let ((dict (make-hash-table :test #'equal)))
    (with-open-file (str path :direction :input)
      (do ((line (read-line str nil 'eof)
		 (read-line str nil 'eof)))
	  ((eql line 'eof))
	(let ((letters (string->sorted-list line)))
	  (push line (gethash letters dict)))))      
    dict))

(defun get-dict ()
  (if (not *dict*)
      (setf *dict* (read-dict *dict_path*))
      *dict*))

(defun string->sorted-list (str)
 (sort 
  (coerce (string-downcase str) 
	  'list) 
  #'char<)) 

(defun find-word-for-letters ()
  (format t "Enter the letters~%")
  (let* ((chars (read))
         (letters (string->sorted-list chars))) 
    (format t "finding ~A in dictionary" letters)
    (gethash letters (get-dict))))



